package com.kyc.kycxkplaygame.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import com.kyc.kycxkplaygame.R;
import com.kyc.kycxkplaygame.adapter.MyBackPageAdapter;
import com.kyc.kycxkplaygame.dbutils.GetPropLevelUtils;
import com.kyc.kycxkplaygame.dbutils.IntImp.AlasbusyIApi;
import com.kyc.kycxkplaygame.dbutils.inter.HttpQuestResultListener;
import com.kyc.kycxkplaygame.entity.UsersProps;
import com.kyc.kycxkplaygame.greendao.PropInfoBean;
import com.kyc.kycxkplaygame.view.GridSpacingItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class MyBackPageActivity extends Activity implements HttpQuestResultListener {

    private RecyclerView recyclerView;
    private MyBackPageAdapter myBackPageAdapter;
    private AlasbusyIApi alasbusyIApi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setFinishOnTouchOutside(false);
        setContentView(R.layout.activity_my_back_page);
        Window window = getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.alpha = 1f;
        window.setAttributes(layoutParams);

        recyclerView = findViewById(R.id.recyclerView);
        //linearLayoutManager = new LinearLayoutManager(getContext());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.addItemDecoration(new GridSpacingItemDecoration(4, 15, false));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        myBackPageAdapter = new MyBackPageAdapter(this);
        recyclerView.setAdapter(myBackPageAdapter);

        findViewById(R.id.backpage_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyBackPageActivity.this.finish();
            }
        });

        alasbusyIApi = AlasbusyIApi.getInstance(this);
        alasbusyIApi.setHttpQuestResultListener(this);

        initData();
    }

    private void initData() {
        //{"体力", "葡萄", "草莓", "西柚", "青苹果", "西瓜", "提示", "炸弹", "时间"};
        /*int zhandan = Integer.parseInt(GetPropLevelUtils.getInstance().getPropNum("炸弹"));//(int) SharedPreferencesUtils.getParam(MyBackPageActivity.this, "zhandan", 0);
        int tishi = Integer.parseInt(GetPropLevelUtils.getInstance().getPropNum("提示"));//(int) SharedPreferencesUtils.getParam(MyBackPageActivity.this, "tishi", 0);
        int time = Integer.parseInt(GetPropLevelUtils.getInstance().getPropNum("时间"));//(int) SharedPreferencesUtils.getParam(MyBackPageActivity.this, "time", 0);
        int xiyou = Integer.parseInt(GetPropLevelUtils.getInstance().getPropNum("西柚"));
        int apple = Integer.parseInt(GetPropLevelUtils.getInstance().getPropNum("青苹果"));
        int xigua = Integer.parseInt(GetPropLevelUtils.getInstance().getPropNum("西瓜"));*/
        alasbusyIApi.findUserProp(GetPropLevelUtils.getInstance().getUserName());
        /*List<PropInfoBean> propInfo = PropInfoManager.getInstance().getPropInfo();
        List<PropInfoBean> list = new ArrayList<>();
        if (propInfo.size() > 0) {
            for (PropInfoBean bean : propInfo) {
                if (!bean.getPropName().equals("体力") && !bean.getPropName().equals("葡萄") && !bean.getPropName().equals("草莓")) {
                    if (bean.getPropNum() > 0) {
                        list.add(bean);
                    }
                }
            }
        }
        myBackPageAdapter.setList(list);
        myBackPageAdapter.notifyDataSetChanged();*/
    }

    @Override
    public void onError(Object e) {

    }

    @Override
    public void onSuccess(Object object) {
        if(object instanceof UsersProps) {
            UsersProps usersProps = (UsersProps) object;
            List<UsersProps.UserPropsBean> userProps = usersProps.getUserProps();
            List<PropInfoBean> list = new ArrayList<>();
            if (userProps.size() > 0) {
                for (UsersProps.UserPropsBean bean : userProps) {
                    if (!bean.getPropName().equals("体力") && !bean.getPropName().equals("葡萄") && !bean.getPropName().equals("草莓")) {
                        if (bean.getPropNum() > 0) {
                            PropInfoBean propInfoBean = new PropInfoBean();
                            propInfoBean.setUserName(GetPropLevelUtils.getInstance().getUserName());
                            propInfoBean.setPropName(bean.getPropName());
                            propInfoBean.setPropNum(bean.getPropNum());
                            propInfoBean.setId(bean.getUpID());
                            list.add(propInfoBean);
                        }
                    }
                }
            }
            myBackPageAdapter.setList(list);
            myBackPageAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onQuestErroCode(Object object) {

    }
}
